import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:image_picker/image_picker.dart';

import 'UploadTM.dart';

class UploadTMBody extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UploadTMBody();
  }
}

class _UploadTMBody extends State<UploadTMBody> {
  final ImagePicker _picker = ImagePicker();

  openAlbum() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      PageUtil().open(context, UploadTM(image));
    }
  }

  openCamera() async {
    final XFile? image = await _picker.pickImage(source: ImageSource.camera);
    if (image!= null) {
      PageUtil().open(context, UploadTM(image));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      LocalImage("bg.png", height: double.infinity, width: double.infinity, fit: BoxFit.cover),
      Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Btn("upload_album.png", "從相簿中選擇", onTap: openAlbum),
        Btn("upload_camera.png", "從相機上傳", onTap: openCamera),
      ])
    ]);
  }
}

class Btn extends StatelessWidget {
  String icon;
  String text;
  Function()? onTap;

  Btn(this.icon, this.text, {this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            margin: EdgeInsets.fromLTRB(40, 20, 40, 20),
            decoration: Global.getWhiteBg_shadow(50, MyColor.DD),
            height: 100,
            width: double.infinity,
            child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              LocalImage(icon, width: 24),
              Container(width: 8),
              Text(text, style: TextStyle(fontSize: 16, color: MyColor.Orange, fontWeight: FontWeight.bold))
            ])));
  }
}
